﻿/*
创建人：zhangrj
日期：2022-05-23
描述：设备负荷预览页面js
*/
var page = {
    url: getRootPath() + "/Ashx/ApsHandler/ApsResourceLoadViewHandler.ashx", //后台处理url
    init: function () {
        page.bind();
    },
    //绑定事件
    bind: function () {
        resTimeView.init(); //初始化日期范围
        //查询信息
        $("#btnSearch").on("click", function () {
            clearMessage();
            page.search();
        });
        //重置查询
        $("#btnReset").on("click", function () {
            clearMessage();
            page.reSet();
        });
        $(document).ready(function () {
            page.reSet();
        });
    },
    //查询
    search: function () {
        var params = {
            action: "SearchResourceLoad",
            startDate: $("#txtStartDate").datebox("getValue"),
            endDate: $("#txtEndDate").datebox("getValue"),
            resourceNum: $("#txtResourceNum").textbox("getValue")
        };
        if (isNullOrWhiteSpaceStr(params.startDate) || isNullOrWhiteSpaceStr(params.endDate)) {
            displayMessage("请选择日期范围", false);
            return;
        }
        if (page.reloadHtml()) { //生成了html,在获取数据
            page.post(page.url, params, true, function (res) {
                if (!res.Result) {
                    displayMessage(res.Message, res.Result);
                    return;
                }
                var xData = [];
                var yData = [];
                var data = res.DBDataSet.dtLine;
                for (var i = 0; i < data.length; i++) {
                    xData.push(data[i].xData);
                    yData.push(data[i].yData);
                }
                resTotalView.init(xData, yData);
                resTimeView.initData(res.DBDataSet.dtResource);
                resWeekView.initData(res.DBDataSet.dtResource);
            });
        }
    },
    //查询重置
    reSet: function () {
        $("#txtResourceNum").textbox("setValue", ""); //设备名
        var startDate = dayjs(new Date()).subtract(30, "day").format("YYYY-MM-DD"); //开始日期往前推30天
        var endDate = dayjs(new Date()).format("YYYY-MM-DD");
        $("#txtStartDate").datebox("setValue", startDate);
        $("#txtEndDate").datebox("setValue", endDate);
    },
    //重置总预览div宽度
    reloadHtml: function () {
        $("#mainView").removeAttr("_echarts_instance_");
        var startDate = dayjs($("#txtStartDate").datebox("getValue"));
        var endDate = dayjs($("#txtEndDate").datebox("getValue"));
        var days = endDate.diff(startDate, 'day');
        var width = days * 50;
        if (width < 1000) {
            $("#mainView").css("width", "");
            return true;
        }
        $("#mainView").css("width", width + "px");
        resWeekView.bind(days);
        return true;
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
};
//总预览
var resTotalView = {
    init: function (xData, yData) {
        var dom = document.getElementById('mainView');
        var myChart = echarts.init(dom);
        var option = {
            color: ['#00DDFF'],
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#6a7985'
                    }
                }
            },
            grid: {
                top: '6%',
                right: '3%',
                left: '0%',
                bottom: '0%',
                containLabel: true
            },
            xAxis: [{
                type: 'category',
                boundaryGap: false,
                axisLabel: {
                    interval: 0 //x轴全部显示
                },
                data: xData
            }],
            yAxis: [{
                type: 'value',
                scale: true,
                max: 100,
                min: 0
            }],
            series: [{
                name: '设备负荷',
                type: 'line',
                stack: 'Total',
                smooth: true,
                lineStyle: {
                    width: 0
                },
                showSymbol: false,
                areaStyle: {
                    opacity: 0.8,
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: 'rgb(128, 255, 165)'
                    },
                    {
                        offset: 1,
                        color: 'rgb(1, 191, 236)'
                    }
                    ])
                },
                emphasis: {
                    focus: 'series'
                },
                data: yData
            }]
        };
        myChart.setOption(option);
        window.addEventListener('resize', myChart.resize);
    }
};
//按时间范围
var resTimeView = {
    init: function () {
        resTimeView.bind();
    },
    bind: function () {
        $("#gdResourceTimeInfo").datagrid({
            columns: [[
                 { field: 'RESOURCENAME', title: '设备编号', width: 180 },
                 { field: 'DESCRIPTION', title: '设备名称', width: 180 },
                 { field: 'TOTALVALUE', title: '能力值', width: 100 },
                 { field: 'USERVALUE', title: '使用能力', width: 100 },
                 { field: 'SURPLUSVALUE', title: '剩余能力', width: 100 },
//                 { field: 'MAXINTERVAL', title: '最大间隙', width: 100 },
//                 { field: 'INTERVALDATE', title: '间隙时间', width: 120 },
                 { field: 'AVAILDRATE', title: '利用率', width: 100 }
                ]]
        });
    },
    initData: function (data) {
        $("#gdResourceTimeInfo").datagrid('loadData', { rows: data });
    }
};
//按周
var resWeekView = {
    bind: function (days) {
        var week = Math.floor(days / 7) + 1;
        var columns = [];
        columns.push({ field: 'RESOURCENAME', title: '设备编码', width: 150 });
        columns.push({ field: 'DESCRIPTION', title: '设备名称', width: 150 });
        for (var i = 1; i <= week; i++) {
            columns.push({ field: 'Week' + i, title: i + '周', width: 100 });
        }
        $("#gdResourceWeekInfo").datagrid({
            columns: [columns]
        });
    },
    initData: function (data) {
        $("#gdResourceWeekInfo").datagrid('loadData', { rows: data });
    }
};
page.init();